import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:wallet_utils/report_widget/base/callback/base_callback.dart';
import 'package:wallet_utils/toast.dart';
import 'package:wallet_utils/webview_common.dart';
import '../../agreement_dialog.dart';
import '../../login_page/constant.dart';
import '../../productPop/product_bootom_sheet.dart';
import '../api/home_api.dart';
import '../bean/auditResultBean.dart';
import '../bean/popUpsBean.dart';
import '../bean/userAgreementBean.dart';
import 'state.dart';

class ResultLogic extends GetxController {
  final ResultState state = ResultState();
  ProductInfo? productInfo;
  List<ProductList>? productList = [];
  List<PopUpsInfo> popUpsInfo=[];

  @override
  void onInit() {
    super.onInit();
    obtainAuditResultInfo();
    getUser2WhereApi();
  }
  //获取用户审核结果
  void obtainAuditResultInfo() async {
    HomeApi.obtainAuditResultInfo(BaseCallback(onSuccess: (map) {
      AuditResultBean auditResultBean = AuditResultBean.fromJson(map);
      if (auditResultBean.data != null) {
        if(auditResultBean.data?.productList!=null){
          productList = auditResultBean.data?.productList;
          update();
        }
      }

    }, onError: (msg) {
      Future.delayed(const Duration(milliseconds: 500), () {
        ToastUtils.showToast(msg ?? "");
      });
    }));
  }

  void getUser2WhereApi() async {
    HomeApi.getUser2WhereApi(BaseCallback(onSuccess: (map) {
      UserAgreementBean userAgreementBean = UserAgreementBean.fromJson(map);
      if (userAgreementBean.data != null) {
        productInfo = userAgreementBean.data;
        state.name=productInfo?.name;
        getCpaPopList();
        update();
      }
    }, onError: (msg) {
      Future.delayed(const Duration(milliseconds: 500), () {
        ToastUtils.showToast(msg ?? "");
      });
    }));
  }

  void popover(int type,int index) {
    Future.delayed(Duration.zero, () {
      showDialog<Null>(
        context: Get.context!,
        barrierDismissible: false,
        useSafeArea: false,
        useRootNavigator: false,
        builder: (BuildContext context) {
          return AgreementDialog(
              name: '',
              content: productInfo?.content ?? '',
              submit: '我已阅读并同意授权',
              isClose: true,
              onTap: () {
                Get.back();
                if(type==2){
                  if(productList!.isNotEmpty){
                    addLoanRecord(productList![index].productId!, ShowPage.result);
                  }
                  Get.to(WebViewCommon(
                    productList![index].linkUrl ?? '',
                  ));
                }
              });
        },
      );
    });
  }

  //埋点
  void addLoanRecord(num productId, int showPage) async {
    HomeApi.addLoanRecord(
        productId,
        showPage,
        BaseCallback(
            onSuccess: (map) {},
            onError: (msg) {
              Future.delayed(const Duration(milliseconds: 500), () {
                ToastUtils.showToast(msg ?? "");
              });
            }));
  }

  //弹窗
  void getCpaPopList() async {
    HomeApi.getCpaPopList(
        PopUps().audit,
        BaseCallback(
            onSuccess: (map) {
              PopUpsBean popUpsBean = PopUpsBean.fromJson(map);
              popUpsInfo = popUpsBean.data!;
              if (popUpsInfo.isNotEmpty) {
                Future.delayed(const Duration(milliseconds: 300), () {
                  ProductBottomSheet.show(
                    Get.context!,
                    logo: productInfo?.imgUrl,
                    name: productInfo?.name,
                    content: productInfo?.content,
                  );
                });
              }
            },
            onError: (msg) {
              Future.delayed(const Duration(milliseconds: 500), () {
                ToastUtils.showToast(msg ?? "");
              });
            }));
  }
}
